package com.gitee.feizns.webtemplate.commons.upload;

import lombok.Getter;

import java.io.InputStream;

/**
 * 上传资源传递数据
 * @author feizns
 * @since 2019/4/14
 */
@Getter
public class UploadEvent {

    /**
     * 上传的类型
     */
    private MediaType mediaType;

    /**
     * 文件名称
     */
    private String filename;

    /**
     * 需要写入的流
     */
    private InputStream inputStream;

    /**
     * 异步
     */
    private boolean isAsync;

    public UploadEvent(String filename, MediaType mediaType, InputStream stream, boolean isAsync) {
        this.filename = filename;
        this.mediaType = mediaType;
        this.inputStream = stream;
        this.isAsync = isAsync;
    }

    public UploadEvent(MediaType mediaType, String filename, InputStream inputStream) {
        this.mediaType = mediaType;
        this.filename = filename;
        this.inputStream = inputStream;
    }

    /**
     * 将流重置，（为流只能使用一次做准备）
     * @param originalEvent
     * @param is
     * @return
     */
    public static final UploadEvent reset(UploadEvent originalEvent, InputStream is) {
        originalEvent.inputStream = is;
        return originalEvent;
    }

}
